<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="100px"
    >
      <el-form-item label="汽车品牌号" prop="carLicense">
        <el-input
          v-model="dataForm.carLicense"
          placeholder="汽车品牌号"
        ></el-input>
      </el-form-item>
      <el-form-item label="汽车颜色" prop="color">
        <el-input v-model="dataForm.color" placeholder="汽车颜色"></el-input>
      </el-form-item>
      <el-form-item label="上牌时间" prop="plateTime">
        <el-date-picker
          v-model="dataForm.plateTime"
          type="datetime"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item label="车型" prop="cheXing">
        <el-select v-model="dataForm.cheXing" placeholder="请选择">
          <el-option
            v-for="item in carConfig.cheXing"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="品牌" prop="carType">
        <el-select v-model="dataForm.carType" placeholder="请选择">
          <el-option
            v-for="item in carConfig.carType"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="排量" prop="paiLiang">
        <el-select v-model="dataForm.paiLiang" placeholder="请选择">
          <el-option
            v-for="item in carConfig.paiLiang"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="能源" prop="nengYuan">
        <el-select v-model="dataForm.nengYuan" placeholder="请选择">
          <el-option
            v-for="item in carConfig.nengYuan"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="国别" prop="guoBie">
        <el-select v-model="dataForm.guoBie" placeholder="请选择">
          <el-option
            v-for="item in carConfig.guoBie"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="结构" prop="jieGou">
        <el-select v-model="dataForm.jieGou" placeholder="请选择">
          <el-option
            v-for="item in carConfig.jieGou"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="驱动" prop="quDone">
        <el-select v-model="dataForm.quDone" placeholder="请选择">
          <el-option
            v-for="item in carConfig.quDone"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="变速箱" prop="bianSuXiang">
        <el-select v-model="dataForm.bianSuXiang" placeholder="请选择">
          <el-option
            v-for="item in carConfig.bianSuXiang"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="生产方式" prop="shengChanFangShi">
        <el-select v-model="dataForm.shengChanFangShi" placeholder="请选择">
          <el-option
            v-for="item in carConfig.shengChanFangShi"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="座位" prop="zuoWei">
        <el-select v-model="dataForm.zuoWei" placeholder="请选择">
          <el-option
            v-for="item in carConfig.zuoWei"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
 import _ from 'lodash';
 import moment from "moment";
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        carId: 0,
        carLicense: "",
        color: "",
        plateTime: "",
        cheXing: "",
        carType: "",
        paiLiang: "",
        nengYuan: "",
        guoBie: "",
        jieGou: "",
        quDone: "",
        bianSuXiang: "",
        shengChanFangShi: "",
        zuoWei: ""
      },
      dataRule: {
        carLicense: [
          { required: true, message: "汽车品牌号不能为空", trigger: "blur" }
        ],
        color: [
          { required: true, message: "汽车颜色不能为空", trigger: "blur" }
        ],
        plateTime: [
          { required: true, message: "上牌时间不能为空", trigger: "blur" }
        ],
        cheXing: [{ required: true, message: "车型不能为空", trigger: "blur" }],
        carType: [{ required: true, message: "品牌不能为空", trigger: "blur" }],
        paiLiang: [
          { required: true, message: "排量不能为空", trigger: "blur" }
        ],
        nengYuan: [
          { required: true, message: "能源不能为空", trigger: "blur" }
        ],
        guoBie: [{ required: true, message: "国别不能为空", trigger: "blur" }],
        jieGou: [{ required: true, message: "结构不能为空", trigger: "blur" }],
        quDone: [{ required: true, message: "驱动不能为空", trigger: "blur" }],
        bianSuXiang: [
          { required: true, message: "变速箱不能为空", trigger: "blur" }
        ],
        shengChanFangShi: [
          { required: true, message: "生产方式不能为空", trigger: "blur" }
        ],
        zuoWei: [{ required: true, message: "座位不能为空", trigger: "blur" }]
      },
      carConfig: {}
    };
  },
  methods: {
    getTagInfo() {
      this.$http({
        url: this.$http.adornUrl(`/car/list/dict/-1`),
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 200) {
          this.carConfig = data.data;
        }
      });
    },
    init(id) {
      this.dataForm.carId = id || 0;
      this.visible = true;
      this.getTagInfo();
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.carId) {
          this.$http({
            url: this.$http.adornUrl(`/car/info/${this.dataForm.carId}`),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 200) {
              this.dataForm=data.data;
              const config=data.data.carDict.split(",");
              this.dataForm.cheXing=config[0];
              this.dataForm.paiLiang=config[1];
              this.dataForm.nengYuan=config[2];
              this.dataForm.guoBie=config[3];
              this.dataForm.jieGou=config[4];
              this.dataForm.quDone=config[5];
              this.dataForm.bianSuXiang=config[6];
              this.dataForm.shengChanFangShi=config[7];
              this.dataForm.zuoWei=config[8];
              this.dataForm.plateTime=moment(data.data.plateTime)
              delete this.dataForm.carDict;
              delete this.dataForm.carDictTrans;              
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          const params=_.cloneDeep(this.dataForm);
           params.userId=this.getUserId();
           params.plateTime=moment(params.plateTime).format("YYYY-MM-DD HH:mm:ss")
           params.carDict=`${params.cheXing},${params.paiLiang},${params.nengYuan},${params.guoBie},${params.jieGou},${params.quDone},${params.bianSuXiang},${params.shengChanFangShi},${params.zuoWei}`;
           delete params.cheXing;
           delete params.paiLiang;
           delete params.nengYuan;
           delete params.guoBie;
           delete params.jieGou;
           delete params.quDone;
           delete params.bianSuXiang;
           delete params.shengChanFangShi;
           delete params.zuoWei;
          this.$http({
            url: this.$http.adornUrl(
              `/car/${!this.dataForm.carId ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData(params)
          }).then(({ data }) => {
            if (data && data.code ===0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
